import React, { Component } from 'react';
import { ChangeSubjectApi,SetDefaultSubApi } from '@/request/api'
import './index.less'
import MyDrawer from '@/components/Change/drawer'


export default class ChangeSubject extends Component {
  state = {
    subjectList: [],
    subItem: {},
    title: '',
    actionCode: ''
  }
  render() {
    return <div className='change_subject'>
      <div className="subject_top">
        <div className="top">
          <section>
            <span>首页显示学科-{this.state.title}</span>
            <span className='change' onClick={this.setDefaultSubFn.bind(this)}>设置默认学科
              <i className='iconfont icon-shangxiazuoyouTriangle12'></i>
            </span>
          </section>
          <h3>
            <i className="iconfont icon-shangxiazuoyouTriangle14"></i>
            <div >
              <MyDrawer title={ this.state.title} getItem={this.GetSubject.bind(this)} /></div>
            <i className="iconfont icon-shangxiazuoyouTriangle12"></i>
          </h3>
        </div>
      </div>
          
      <div className="subject_bottom">
        <h3>专业科目</h3>
        <ul>
          {this.state.subjectList.map(item => (
            <li className='sub_li' key={item.id}>{item.title}</li>
          ))}
        </ul>
      </div>
    </div>;
  }
  setDefaultSubFn() {
    // console.log('设置默认学科', this);
    // console.log('当前的code',this.state.actionCode);
    let code = this.state.actionCode
    console.log('当前code',code);
    SetDefaultSubApi({ code }).then(res => {
      localStorage.setItem('actionCode',code)
      // console.log('SetDefaultSubApi',res);
      this.props.history.push('/home')
    })
    
  }
  componentDidMount() {
    let {actionCode,title} = this.props.location.state
    // console.log('actionCode',actionCode);
    // console.log('localtion.state',this.props.location.state);
    ChangeSubjectApi({actionCode}).then(res => {
      // console.log('change',res);
      this.setState({subjectList: res.data,title})
    })
  }
  GetSubject(item) {
    console.log('父组件item',item);
    let { actionCode, title } = item
    this.setState({ actionCode })
    ChangeSubjectApi({ actionCode }).then(res => {
      // console.log('点击后', res);
      this.setState({subjectList: res.data,title})
    })
    // this.setState({ subItem: item })
    // console.log('GetSubject', item);
    // console.log('this.state.subItem',this.state.subItem);
  }
}
